using UnityEngine;
using System.Collections;

public class UnitNoteDetails : MonoBehaviour
{
	public UnitStatus unitStatus;
	GameObject Perk;
	// Use this for initialization
	void Start ()
	{
		Perk = Resources.Load ("Prefaps/UI/StaticUI/Perk/Perk") as GameObject;
		unitStatus = transform.parent.GetComponent<UnitNote>().unitStatus.GetComponent<UnitStatus>();

		for (int i=0; i < transform.childCount; ++i) {
			if(transform.GetChild(i).GetComponent<UILabel>())
				transform.GetChild(i).GetComponent<UILabel>().depth = transform.parent.GetComponent<UISprite>().depth + 1;
			else if(transform.GetChild(i).GetComponent<UISprite>())
				transform.GetChild(i).GetComponent<UISprite>().depth = transform.parent.GetComponent<UISprite>().depth + 1;
		}

		transform.FindChild ("StatLabel").GetComponent<UILabel>().text = string.Format(xmlLoader.GetInstance().TextLoad(XML_INDEX.STAT_DEF, "Health") + " : \n" +
                                                                                        xmlLoader.GetInstance().TextLoad(XML_INDEX.STAT_DEF, "Morale") + " : \n" +
                                                                                       xmlLoader.GetInstance().TextLoad(XML_INDEX.STAT_DEF, "Speed") + "  : \n\n" +
                                                                                       xmlLoader.GetInstance().TextLoad(XML_INDEX.STAT_DEF, "Accuracy") + " : \n" +
                                                                                       xmlLoader.GetInstance().TextLoad(XML_INDEX.STAT_DEF, "Strength") + " : ");

        transform.FindChild ("WeaponNameLabel").GetComponent<UILabel> ().text = unitStatus.m_WeaponData.m_strWeaponName;
		transform.FindChild ("WeaponInfoLabel").GetComponent<UILabel> ().text = string.Format ("DAMAGE     : {0}\nACCURACY  : {1}\nRATE        : {2}\nAMMO        : {3} / {4}"
                                                                                          , unitStatus.m_WeaponData.m_iWeaponDmg, unitStatus.m_WeaponData.m_iWeaponAccur, unitStatus.m_WeaponData.m_iWeaponRate
		                                                                                  ,unitStatus.m_WeaponData.m_iCurrentAmmo, unitStatus.m_WeaponData.m_iFullAmmo);

		for ( int i =0; i < unitStatus.m_unitData.ListPerk.Count; ++i){
			(Instantiate(Perk) as GameObject).transform.parent =  transform.FindChild("Perks").transform;
			transform.FindChild("Perks").transform.GetChild(i).GetComponent<PerkBtn> ().SetInit ( unitStatus.m_unitData.ListPerk[i], i, PAPER_TYPE.NOTE);
			transform.FindChild("Perks").transform.GetChild(i).GetComponent<UISprite>().depth = transform.parent.GetComponent<UISprite>().depth + 1;
		}

		for (int i = unitStatus.m_unitData.ListPerk.Count; i < 6; ++i) {
			(Instantiate(Perk) as GameObject).transform.parent =  transform.FindChild("Perks").transform;
			transform.FindChild("Perks").transform.GetChild(i).GetComponent<PerkBtn> ().SetEmpty ( i, PAPER_TYPE.NOTE);
			transform.FindChild("Perks").transform.GetChild(i).GetComponent<UISprite>().depth = transform.parent.GetComponent<UISprite>().depth + 1;
		}

        NoteTraitInit();

        StatLabeling("Health", unitStatus.m_unitData);
        StatLabeling("Morale", unitStatus.m_unitData);
        StatLabeling("Speed", unitStatus.m_unitData);
        StatLabeling("Accuracy", unitStatus.m_unitData);
        StatLabeling("Strength", unitStatus.m_unitData);
	}

    public void StatLabeling(string strStat, UnitData m_UnitData, int iColor = 0) 
    {
        int iOverall = 0;

        switch (strStat)
        {
            case "Health":
                 for (int i = 0; i < m_UnitData.arrayTrait.Length; ++i)
                 {
                     if (unitStatus.m_unitData.arrayTrait[i] != null)
                        iOverall += m_UnitData.arrayTrait[i].m_iHealth;

                 }
                 for (int i = 0; i < m_UnitData.ListPerk.Count; ++i)
                 {
                     if (m_UnitData.ListPerk[i] != null)
                         iOverall += m_UnitData.ListPerk[i].m_iHealth;
                 }

                iOverall += m_UnitData.iHP;

                if (iOverall > m_UnitData.iHP)
                    transform.Find("StatLabel").FindChild(string.Format("{0}Label", strStat)).GetComponent<UILabel>().color = new Color(75 / 255f, 150 / 255f, 75 / 255f);
                else if (iOverall == m_UnitData.iHP)
                    transform.Find("StatLabel").FindChild(string.Format("{0}Label", strStat)).GetComponent<UILabel>().color = new Color(58 / 255f, 39 / 255f, 24 / 255f);
                else
                    transform.Find("StatLabel").FindChild(string.Format("{0}Label", strStat)).GetComponent<UILabel>().color = new Color(212 / 255f, 117 / 255f, 117 / 255f);

                break;

            case "Morale":
                for (int i = 0; i < m_UnitData.arrayTrait.Length; ++i)
                 {
                     if (unitStatus.m_unitData.arrayTrait[i] != null)
                        iOverall += m_UnitData.arrayTrait[i].m_iMorale;
                 }
                for (int i = 0; i < m_UnitData.ListPerk.Count; ++i)
                {
                    if (m_UnitData.ListPerk[i] != null)
                        iOverall += m_UnitData.ListPerk[i].m_iMorale;
                }

                iOverall += m_UnitData.iMorale;

                if (iOverall > m_UnitData.iMorale)
                    transform.Find("StatLabel").FindChild(string.Format("{0}Label", strStat)).GetComponent<UILabel>().color = new Color(75 / 255f, 150 / 255f, 75 / 255f);
                else if (iOverall == m_UnitData.iMorale)
                    transform.Find("StatLabel").FindChild(string.Format("{0}Label", strStat)).GetComponent<UILabel>().color = new Color(58 / 255f, 39 / 255f, 24 / 255f);
                else
                    transform.Find("StatLabel").FindChild(string.Format("{0}Label", strStat)).GetComponent<UILabel>().color = new Color(212 / 255f, 117 / 255f, 117 / 255f);

                break;

            case "Speed":
                for (int i = 0; i < m_UnitData.arrayTrait.Length; ++i)
                 {
                     if (unitStatus.m_unitData.arrayTrait[i] != null)
                        iOverall += m_UnitData.arrayTrait[i].m_iSpeed;
                 }
                for (int i = 0; i < m_UnitData.ListPerk.Count; ++i)
                {
                    if (m_UnitData.ListPerk[i] != null)
                        iOverall += m_UnitData.ListPerk[i].m_iSpeed;
                }

                iOverall += m_UnitData.iSpeedStat;

                if (iOverall > m_UnitData.iSpeedStat)
                    transform.Find("StatLabel").FindChild(string.Format("{0}Label", strStat)).GetComponent<UILabel>().color = new Color(75 / 255f, 150 / 255f, 75 / 255f);
                else if (iOverall == m_UnitData.iSpeedStat)
                    transform.Find("StatLabel").FindChild(string.Format("{0}Label", strStat)).GetComponent<UILabel>().color = new Color(58 / 255f, 39 / 255f, 24 / 255f);
                else
                    transform.Find("StatLabel").FindChild(string.Format("{0}Label", strStat)).GetComponent<UILabel>().color = new Color(212 / 255f, 117 / 255f, 117 / 255f);

                break;

            case "Accuracy":
                for (int i = 0; i < m_UnitData.arrayTrait.Length; ++i)
                 {
                     if (unitStatus.m_unitData.arrayTrait[i] != null)
                        iOverall += m_UnitData.arrayTrait[i].m_iAccuracy;
                 }
                for (int i = 0; i < m_UnitData.ListPerk.Count; ++i)
                {
                    if (m_UnitData.ListPerk[i] != null)
                        iOverall += m_UnitData.ListPerk[i].m_iAccuracy;
                }

                iOverall += m_UnitData.iAccur;

                if (iOverall > m_UnitData.iAccur)
                    transform.Find("StatLabel").FindChild(string.Format("{0}Label", strStat)).GetComponent<UILabel>().color = new Color(75 / 255f, 150 / 255f, 75 / 255f);
                else if (iOverall == m_UnitData.iAccur)
                    transform.Find("StatLabel").FindChild(string.Format("{0}Label", strStat)).GetComponent<UILabel>().color = new Color(58 / 255f, 39 / 255f, 24 / 255f);
                else
                    transform.Find("StatLabel").FindChild(string.Format("{0}Label", strStat)).GetComponent<UILabel>().color = new Color(212 / 255f, 117 / 255f, 117 / 255f);

                break;

            case "Strength":
                for (int i = 0; i < m_UnitData.arrayTrait.Length; ++i)
                 {
                     if (unitStatus.m_unitData.arrayTrait[i] != null)
                        iOverall += m_UnitData.arrayTrait[i].m_iStrength;
                 }
                for (int i = 0; i < m_UnitData.ListPerk.Count; ++i)
                {
                    if (m_UnitData.ListPerk[i] != null)
                        iOverall += m_UnitData.ListPerk[i].m_iStrength;
                }

                iOverall += m_UnitData.iStrength;

                if (iOverall > m_UnitData.iStrength)
                    transform.Find("StatLabel").FindChild(string.Format("{0}Label", strStat)).GetComponent<UILabel>().color = new Color(75 / 255f, 150 / 255f, 75 / 255f);
                else if (iOverall == m_UnitData.iStrength)
                    transform.Find("StatLabel").FindChild(string.Format("{0}Label", strStat)).GetComponent<UILabel>().color = new Color(58 / 255f, 39 / 255f, 24 / 255f);
                else
                    transform.Find("StatLabel").FindChild(string.Format("{0}Label", strStat)).GetComponent<UILabel>().color = new Color(212 / 255f, 117 / 255f, 117 / 255f);

                break;
        }
        transform.Find("StatLabel").FindChild(string.Format("{0}Label", strStat)).GetComponent<StatHover>().m_UnitData = m_UnitData;
        transform.Find("StatLabel").FindChild(string.Format("{0}Label", strStat)).GetComponent<StatHover>().InfoInitialize();
        transform.Find("StatLabel").FindChild(string.Format("{0}Label", strStat)).GetComponent<UILabel>().text = string.Format("{0}", iOverall);
         

    }

    // Update is called once per frame
    void Update()
	{
        StatLabeling("Health", unitStatus.m_unitData);
        StatLabeling("Morale", unitStatus.m_unitData);
        StatLabeling("Speed", unitStatus.m_unitData);
        StatLabeling("Accuracy", unitStatus.m_unitData);
        StatLabeling("Strength", unitStatus.m_unitData);
	}

    public void NoteTraitInit()
    {
        if (unitStatus.m_unitData.iTraitPoint > 0)
        {
            transform.FindChild("AddTraitLight").gameObject.SetActive(true);
        }
        else
        {
            transform.FindChild("AddTraitLight").gameObject.SetActive(false);
        }

        for (int i = 0; i < 4; ++i)
        {
            if (unitStatus.m_unitData.arrayTrait[i] != null)
            {
                if (unitStatus.m_unitData.arrayTrait[i].m_strName != "")
                {
                    GameObject tempTrait;
                    tempTrait = (Instantiate(Resources.Load("Prefaps/UI/StaticUI/UnitNote/NoteTrait")) as GameObject).gameObject;
                    tempTrait.transform.parent = transform.FindChild("Traits").transform;
                    if (i != 0)
                        tempTrait.GetComponent<NoteTrait>().m_bTrait = true;

                    tempTrait.GetComponent<NoteTrait>().m_iIndx = i;
                    tempTrait.GetComponent<NoteTrait>().m_traitData = unitStatus.m_unitData.arrayTrait[i];
                }
            }
        }
    }
}

